﻿using System.Collections;
using System.Collections.Generic;
using System.Threading;

namespace CoreLibrary.Collection {

    public class ReadWriteList<T> : IList<T> {

        private readonly List<T> _list = new List<T>();
        public readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();

        public int Count {
            get {
                Lock.EnterReadLock();
                try {
                    return _list.Count;
                } 
                finally {
                    Lock.ExitReadLock();
                }
            }
        }

        public bool IsReadOnly {
            get {
                return false;
            }
        }

        public IEnumerator<T> GetEnumerator() {
            Lock.EnterReadLock();
            try {
                return _list.GetEnumerator();
            }
            finally {
                Lock.ExitReadLock();
            }
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }

        public void Add(T item) {
            Lock.EnterWriteLock();
            try {
                _list.Add(item);
            }
            finally {
                Lock.ExitWriteLock();
            }
        }

        public void Clear() {
            Lock.EnterWriteLock();
            try {
                _list.Clear();
            }
            finally {
                Lock.ExitWriteLock();
            }
        }

        public bool Contains(T item) {
            Lock.EnterReadLock();
            try {
                return _list.Contains(item);
            }
            finally {
                Lock.ExitReadLock();
            }
        }

        public void CopyTo(T[] array, int arrayIndex) {
            Lock.EnterReadLock();
            try {
                _list.CopyTo(array, arrayIndex);
            }
            finally {
                Lock.ExitReadLock();
            }
        }

        public bool Remove(T item) {
            Lock.EnterWriteLock();
            try {
                return _list.Remove(item);
            }
            finally {
                Lock.ExitWriteLock();
            }
        }

        public int IndexOf(T item) {
            Lock.EnterReadLock();
            try {
                return _list.IndexOf(item);
            }
            finally {
                Lock.ExitReadLock();
            }
        }

        public void Insert(int index, T item) {
            Lock.EnterWriteLock();
            try {
                _list.Insert(index, item);
            }
            finally {
                Lock.ExitWriteLock();
            }
        }

        public void RemoveAt(int index) {
            Lock.EnterWriteLock();
            try {
                RemoveAt(index);
            }
            finally {
                Lock.ExitWriteLock();
            }
        }

        public T this[int index] {
            get {
                Lock.EnterReadLock();
                try {
                    return _list[index];
                }
                finally {
                    Lock.ExitReadLock();
                }
            }
            set {
                Lock.EnterWriteLock();
                try {
                    _list[index] = value;
                }
                finally {
                    Lock.ExitWriteLock();
                }
            }
        }
    }
}